package com.system.insurance.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.system.common.result.Result;
import com.system.insurance.entity.BigInsuranceType;
import com.system.insurance.entity.SmallInsuranceType;
import com.system.insurance.service.BigInsuranceTypeService;
import com.system.insurance.service.ContentSettingService;
import com.system.insurance.service.SmallInsuranceTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.bouncycastle.asn1.cms.PasswordRecipientInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 大保险类型 前端控制器
 * </p>
 *
 * @author WS
 * @since 2024-05-10
 */
@Api(tags = "大保险类型")
@RestController
@RequestMapping("/insurance/big-insurance-type")
public class BigInsuranceTypeController {

    @Autowired
    private BigInsuranceTypeService bigInsuranceTypeService;

    @Autowired
    private SmallInsuranceTypeService smallInsuranceTypeService;

    @Autowired
    private ContentSettingService contentSettingService;

    @ApiOperation(value = "查看所有大保险类")
    @GetMapping("/bigInsuranceTypeList")
    public Result<List<BigInsuranceType>> bigInsuranceTypeList(){
        List<BigInsuranceType> list = bigInsuranceTypeService.list();
        return Result.ok(list);
    }

    @ApiOperation(value = "添加大保险种类")
    @PostMapping("/saveBigInsuranceType")
    public Result<BigInsuranceType> saveBigInsuranceType(@RequestBody BigInsuranceType bigInsuranceType){
        bigInsuranceType.setCreateTime(LocalDateTime.now());
        boolean save = bigInsuranceTypeService.save(bigInsuranceType);

        if (save){
            return Result.ok(bigInsuranceType).message("添加成功");
        }
        return Result.fail(bigInsuranceType).message("添加失败");
    }

    @ApiOperation(value = "修改大保险种类")
    @PostMapping("/updateBigInsuranceType")
    public Result<BigInsuranceType> updateBigInsuranceType(@RequestBody BigInsuranceType bigInsuranceType){
        bigInsuranceType.setUpdateTime(LocalDateTime.now());
        boolean update = bigInsuranceTypeService.updateById(bigInsuranceType);

        if (update){
            return Result.ok(bigInsuranceType).message("修改成功");
        }
        return Result.fail(bigInsuranceType).message("修改失败");
    }

    @ApiOperation(value = "删除大保险种类")
    @PostMapping("/deleteBigInsuranceType")
    public Result deleteBigInsuranceType(@RequestBody BigInsuranceType bigInsuranceType){
        //删除大保险种类，首先判断是否有下级，有下级概况，优先删除下级，不删除提示不能删除该种类。
        LambdaQueryWrapper<SmallInsuranceType> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SmallInsuranceType::getBigid,bigInsuranceType.getId());
        List<SmallInsuranceType> list = smallInsuranceTypeService.list(wrapper);

        if (list.size()>0){
            return Result.fail().message("不可删除，请优先删除“二级保险中”数据在进行删除。");
        }else if (list.size()==0){
            boolean remove = bigInsuranceTypeService.removeById(bigInsuranceType.getId());
            if (remove){
                return Result.ok().message("删除成功");
            }
            return Result.fail().message("删除失败");
        }

//
//        boolean remove = bigInsuranceTypeService.removeById(bigInsuranceType.getId());
//
//        if (remove){
//            return Result.ok().message("删除成功");
//        }
        return Result.fail().message("删除失败");
    }

    @ApiOperation(value = "分页查询")
    @GetMapping("/{page}/{limit}")
    public Result<IPage<BigInsuranceType>> index22(@PathVariable Long page, @PathVariable Long limit, @RequestParam(required = false) String name){

        Page<BigInsuranceType> recommendPage = new Page<>(page,limit);


        if (StringUtils.isEmpty(name)) {

            IPage<BigInsuranceType> iPage = bigInsuranceTypeService.page(recommendPage);

            return Result.ok(iPage);
        }else {
            LambdaQueryWrapper<BigInsuranceType> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(BigInsuranceType::getName,name);

            IPage<BigInsuranceType> iPage = bigInsuranceTypeService.page(recommendPage, wrapper);

            return Result.ok(iPage);
        }
    }

}

